package com.moli.iov.protocol.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.moli.iov.protocol.codec.DataDecoder;
import com.moli.iov.protocol.model.Header;
import com.moli.iov.protocol.model.HeartBeatReq;
import com.moli.iov.protocol.model.ReSendInfoReq;
import com.moli.iov.protocol.model.VehicleLoginReq;
import com.moli.iov.protocol.model.VehicleLogoutReq;
import com.moli.iov.protocol.model.realtime.RealTimeInfoReq;
import com.moli.iov.util.ByteArrayUtil;

public class BatchProtocolUtil {

	public static void main(String[] args) throws Exception {

		String fileName = "E:/mlcx/project/moli-iov/iov-common/iov-common-protocol/src/main/java/com/moli/iov/protocol/util/ArrayPro.txt";
		BatchProtocolUtil.readToString(fileName );
	}


	public static String readToString(String fileName) throws Exception {  


		try {  
			FileReader reader = new FileReader(fileName);
			BufferedReader bufferedReader = new BufferedReader(reader);
			String line = null;
			while((line=bufferedReader.readLine())!=null){
				if(line.indexOf("Send:")>0){
					//        			System.out.println(line.substring(line.indexOf("Send:")+5));
				}
				//        		System.out.println(line);
				if(line.indexOf("Recv:")>0){
					//        			System.out.println(line.substring(line.indexOf("Recv:")+5));
					line = line.substring(line.indexOf("Recv:")+5);
					line = line.replaceAll("\r|\n", "");
					line = line.replaceAll(" ", "");

					byte[] bytes = ByteArrayUtil.hexStringToBytes(line);

					DataDecoder<Header> ddh = new DataDecoder<Header>();
					Header header = new Header();
					ddh.decode(header, bytes);
					
					switch (header.commandTag) {
					case HEARTBEAT:
						parse(HeartBeatReq.class,bytes);
						break;
					case PLATFORM_LOGIN:

						break;
					case PLATFORM_LOOUT:

						break;
					case QUERY_CMD:

						break;
					case REALTIME_INFO_REPORT:
						parse(RealTimeInfoReq.class, bytes);
						break;
					case RESEND_INFO_REPORT:
						parse(ReSendInfoReq.class,bytes);
						break;
					case SETTING_CMD:
						//设置参数
						break;
					case TERMINAL_CONTROL_CMD:
						//车载终端控制命令
						break;
					case TERMINAL_TIME_CHECK:
						//TODO 终端校时
						break;
					case VEHICLE_LOGIN:
						//车辆登入
						parse(VehicleLoginReq.class, bytes);
						break;
					case VEHICLE_LOGIN_RESP:

						break;
					case VEHICLE_LOOUT:
						//车辆登出
						parse(VehicleLogoutReq.class, bytes);
						break;
					case VEHICLE_LOOUT_RESP:

						break;
					default:
						break;
					}


				}
			}
			bufferedReader.close();
		} catch (FileNotFoundException e) {  
			e.printStackTrace();  
		} catch (IOException e) {  
			e.printStackTrace();  
		}  
		//        try {  
		//        	String str = new String(filecontent, encoding);
		//        	
		//        	str = str.replaceAll("\r|\n", "");
		//        	str = str.replaceAll(" ", "");
		//        	System.out.println(str);
		//            return str;  
		//        } catch (UnsupportedEncodingException e) {  
		//            System.err.println("The OS does not support " + encoding);  
		//            e.printStackTrace();  
		//            return null;  
		//        }  
		return null;
	}  
	
	static <T> T parse(Class<T> clazz,byte[] bytes) throws InstantiationException, IllegalAccessException, Exception{
		DataDecoder<T> decoder = new DataDecoder<>();
		T a = clazz.newInstance();
		decoder.decode(a, bytes);
//		System.out.println(a.toString());
		return a;
	}
	
}
